package com.shtel.paas.monitorcenter.process.management.service.processexporter;

/**
 * @ClassName
 * @Description Too
 * @Author ME
 * @Data 17:41
 **/

import java.util.*;

import com.jcraft.jsch.JSchException;

import com.shtel.paas.monitorcenter.process.management.entity.ProcessInfoParam;
import com.shtel.paas.monitorcenter.process.management.service.util.sshtool.SSHMultiCommandExecutor;
import io.prometheus.client.Collector;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class ProcessGaugeExports extends Collector {


    private static ProcessInfoParam param;

    public void setParam(ProcessInfoParam param) {
        this.param = param;
    }

    public List<ProcessInfoParam> lis = new ArrayList<>();

//    static
//    static{
//        //todo 改为读配置文件yml
//        pss.add(new Process("10.145.247.140", "9092"));
//        pss.add(new Process("10.145.242.28", "3968"));
//
//        pss.add(new Process("10.7.98.42","9081"));
//		pss.add(new ProcessInfoParam("10.7.98.43","9081"));
//		pss.add(new ProcessInfoParam("10.7.98.44","9081"));
//		pss.add(new ProcessInfoParam("10.7.98.45","9081"));
//		pss.add(new ProcessInfoParam("10.7.98.46","9081"));
//		pss.add(new ProcessInfoParam("10.7.98.47","9081"));
//		pss.add(new ProcessInfoParam("10.7.98.48","9081"));
//		pss.add(new ProcessInfoParam("10.7.98.49","9081"));
//
//		pss.add(new ProcessInfoParam("10.145.246.186","9081"));
//
//
//		pss.add(new ProcessInfoParam("10.7.73.201","2321"));  //windows
//		pss.add(new ProcessInfoParam("10.7.73.202","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.203","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.204","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.205","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.206","2321"));

//		pss.add(new ProcessInfoParam("10.7.73.96","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.97","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.98","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.99","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.100","2321"));
//		pss.add(new ProcessInfoParam("10.7.73.101","2321"));
//
//		pss.add(new ProcessInfoParam("10.7.95.96","2321"));
//		pss.add(new ProcessInfoParam("10.7.95.97","2321"));
//		pss.add(new ProcessInfoParam("10.7.95.98","2321"));
//		pss.add(new ProcessInfoParam("10.7.95.99","2321"));
//		pss.add(new ProcessInfoParam("10.7.95.100","2321"));
//		pss.add(new ProcessInfoParam("10.7.95.101","2321"));

//        pss.add(new Process("10.7.82.22","2321"));
//
//        pss.add(new Process("10.7.21.176", "7005"));
//        //pss.add(new ProcessInfoParam("10.7.33.5", "8001"));   //aix
//        pss.add(new Process("10.7.82.22", "10080"));
//    }

    @Override
    public List<MetricFamilySamples> collect() {

        List<ProcessInfoParam> pss = new LinkedList<ProcessInfoParam>();        // TODO Auto-generated method stub
        pss.add(param);

        List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();

//		GaugeMetricFamily used = new GaugeMetricFamily(
//		        "hahahaha",
//		        "Used bytes of a ssss JVM memory pool.",
//		        Collections.singletonList("pool"));
//		used.addMetric(Collections.singletonList("heap"), 100000);
//
//		mfs.add(used);
        //遍历 process ssh 获得指标
        List<String> ls = new ArrayList<String>();
        ls.add("ip");
        ls.add("port");
        ls.add("pid");
        ls.add("usr");
        ls.add("cpu");
        ls.add("mem");
        ls.add("start");
        ls.add("cmd");
       /* GaugeMetricFamily cpu_used = new GaugeMetricFamily(
                "process_union",
                "some informations of specified process.",
                ls);*/
        for (ProcessInfoParam p : pss) {
            try {
                SSHMultiCommandExecutor sshExecutor=new SSHMultiCommandExecutor(p.ip, p.port);
//                if(StringUtils.isBlank(p.pid))
//                {
//                     sshExecutor = ;
//
//                }else{
//                     sshExecutor = new SSHMultiCommandExecutor(p.ip,null,p.pid);
//
//                }

                System.out.println("ip:"+sshExecutor.ip+"/port:"+sshExecutor.port+"/pid:"+sshExecutor.pid);
                SSHMultiCommandExecutor res = sshExecutor.execute();

                if (res != null) {
                    List<String> vs = new ArrayList<String>();
                    vs.add(res.ip);
                    vs.add(res.port);
                    vs.add(res.pid);
                    vs.add(res.user);
                    vs.add(res.cpu_use);
                    vs.add(res.memory_use);
                    vs.add(res.start_time);
                    vs.add(res.cmd);

                    ProcessInfoParam process = new ProcessInfoParam();
                    process.setIp(res.ip);
                    process.setPort(res.port);
                    process.setPid(res.pid);
                    process.setUser(res.user);
                    process.setCpuUse(res.cpu_use);
                    process.setMemoryUse(res.memory_use);
                    process.setStartTime(res.start_time);
                    process.setCmd(res.cmd);
                    lis.add(process);

                    // cpu_used.addMetric(vs, 100000);
                    System.out.println("-----------------------------------" + vs);
                    //mfs.add(cpu_used);
                } else {
                    System.err.println(p.ip + " " + p.port + " process not exists !");
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(p.ip + "  connected refused!");
            }
        }

//		//SSHMultiCommandExecutor sshExecutor = new SSHMultiCommandExecutor("10.145.242.13","50070", "spark", "Spar1oon");
//		SSHMultiCommandExecutor sshExecutor = new SSHMultiCommandExecutor("10.7.82.22","10080", "patrol", "patrol-1");
//
//		//根据端口取 pid
//        SSHMultiCommandExecutor res;
//		try {
//			res = sshExecutor.execute();
//			List<String> ls = new ArrayList<String>();
//	        ls.add("ip");
//	        ls.add("port");
//	        ls.add("pid");
//	        ls.add("usr");
//	        ls.add("cpu");
//	        ls.add("mem");
//			ls.add("start");
//			ls.add("cmd");
//			GaugeMetricFamily cpu_used = new GaugeMetricFamily(
//			        "process_union",
//			        "some informations of specified process.",
//			         ls );
//			List<String> vs = new ArrayList<String>();
//			vs.add(res.ip);
//			vs.add(res.port);
//			vs.add(res.pid);
//			vs.add(res.user);
//			vs.add(res.cpu_use);
//			vs.add(res.memory_use);
//			vs.add(res.start_time);
//			vs.add(res.cmd);
//			cpu_used.addMetric(vs, 100000);
//
//			mfs.add(cpu_used);
//		} catch (JSchException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}


        return mfs;
    }

}